﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;
using System.Threading;

namespace PepsiTwitterFeed
{
    public partial class Form1 : Form
    {
        string m_ConnectionString = @"Data Source=SQL01;Initial Catalog=SuperSign;Persist Security Info=True;User ID=sa ; Password=sa";
        public Form1()
        {
            InitializeComponent();
        }

        private void Bt_Start_Click(object sender, EventArgs e)
        {
            Bt_stop.Enabled = true;
            Bt_Start.Enabled = false;

            //GetFeed();
            Thread pepsiFeed = new Thread(new ThreadStart(GetFeed));
            pepsiFeed.Start();
        }

        private void GetFeed()
        {
            while (!Bt_Start.Enabled)
            {
                string colorBlue = "[Color Val=\"#0000ff\"]";
                string colorRed = "[Color Val=\"#d72827\"]";
                string colorPurple = "[Color Val=\"#BA55D3\"]";
                string imageTag = "[IMAGETAG ID=\"71\"]";
                string ribbon8 = "";
                string ribbon9 = "";

                try
                {
                    XmlDocument xdoc = new XmlDocument();

                    xdoc.Load("http://a.abcnews.com/xml/elections/2012/dynamic/dynamicgeneral/prod/elections/ribbon/DigitalBoard.xml");
                    //xdoc.Load(@"DigitalBoard.xml");

                    XmlNodeList nList8 = xdoc.SelectNodes("/DigitalBoard/Ribbon8/Item");
                    XmlNodeList nList9 = xdoc.SelectNodes("/DigitalBoard/Ribbon9/Item");

  

                    string miniSpace = "  ";
                    string LargeSpace = "      ";
                   

                    for (int i = 0; i < nList8.Count; i++)
                    {
                        XmlNode msg = nList8[i];

                        string itemLabel = "[Color Val=\"#FFFFFF\"]" + msg.Attributes["label"].InnerText;
                        string itemPollsClosing = msg.Attributes["pollsClosing"].InnerText;
                        string CandMSG = "";

                        if (itemPollsClosing != "")
                        {
                            foreach (XmlNode msgChild in msg.ChildNodes)
                            {
                                string CandName = msgChild.Attributes["name"].InnerText;
                                string CandParty = msgChild.Attributes["party"].InnerText;
                                string CandColor;
                                if (CandParty == "R")
                                    CandColor = colorRed;
                                else if (CandParty == "D")
                                    CandColor = colorBlue;
                                else
                                    CandColor = colorPurple;

                                string CandWin = msgChild.Attributes["win"].InnerText;
                                string CandTick = "";

                                if (CandWin != "N")
                                    CandMSG += miniSpace + imageTag + CandColor + CandTick + CandName;
                                else
                                    CandMSG += miniSpace + CandColor + CandTick + CandName;
                            }
                            ribbon8 += LargeSpace + itemLabel + CandMSG;
                        }
                        else
                            ribbon8 += LargeSpace + itemLabel;
                    }

                    for (int i = 0; i < nList9.Count; i++)
                    {
                        XmlNode msg = nList9[i];

                        string itemLabel = "[Color Val=\"#FFFFFF\"]" + msg.Attributes["label"].InnerText;
                        string itemPollsClosing = msg.Attributes["pollsClosing"].InnerText;
                        string CandMSG = "";

                        if (itemPollsClosing != "")
                        {
                            foreach (XmlNode msgChild in msg.ChildNodes)
                            {
                                string CandName = msgChild.Attributes["name"].InnerText;
                                string CandParty = msgChild.Attributes["party"].InnerText;
                                string CandColor;
                                if (CandParty == "R")
                                    CandColor = colorRed;
                                else if (CandParty == "D")
                                    CandColor = colorBlue;
                                else
                                    CandColor = colorPurple;

                                string CandWin = msgChild.Attributes["win"].InnerText;
                                string CandTick = "";

                                if (CandWin != "N")
                                    CandMSG += miniSpace + imageTag + CandColor + CandTick + CandName;
                                else
                                    CandMSG += miniSpace + CandColor + CandTick + CandName;
                            }
                            ribbon9 += LargeSpace + itemLabel + CandMSG;
                        }
                        else
                            ribbon9 += LargeSpace +  itemLabel;
                    }
                }
                catch (Exception e)
                {
                    string error = e.Message;
                    ribbon8 = "[Color Val=\"#FFFFFF\"]   Welcome to Times Square   ";
                    ribbon9 = "[Color Val=\"#FFFFFF\"]   Welcome to Times Square   ";
                }
                finally
                {
                    if(ribbon8 =="")
                        ribbon8 = "[Color Val=\"#FFFFFF\"]   Welcome to Times Square  ";
                    if( ribbon9 == "")
                        ribbon9 = "[Color Val=\"#FFFFFF\"]   Welcome to Times Square  ";
                }


                UpdateToDB(ribbon8, "[Election2012_8]");
                UpdateToDB(ribbon9, "[Election2012_9]");
                Thread.Sleep(5000);
            }
        }

        private void UpdateToDB(string dataTagInfo, string TageName)
        {
            SqlConnection conn = new SqlConnection(m_ConnectionString);
            try
            {
   

                conn.Open();

                string sqlcmd = "update DataTag set LastValue = '" + dataTagInfo.Replace("'", "''") + "' where Name = '" + TageName + "'";

                SqlCommand cmd = new SqlCommand(sqlcmd, conn);
                cmd.ExecuteNonQuery();


            }
            catch (Exception e)
            {
            }
            conn.Close();
        }

        private void Bt_stop_Click(object sender, EventArgs e)
        {
            Bt_Start.Enabled = true;
 
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Bt_Start.Enabled = true;
        }


    }
}
